package com.jvtd.umeng;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.DrawableRes;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareConfig;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class JvtdUM {
    private static volatile JvtdUM instance;
    private final Context mContext;
    private List<SHARE_MEDIA> mShareList = new ArrayList<>();
    private UMShareAPI mShareApi;

    public static JvtdUM getInstance(Context context) {
        if (instance == null)
            synchronized (JvtdUM.class) {
                if (instance == null)
                    instance = new JvtdUM(context.getApplicationContext());
            }
        return instance;
    }

    private JvtdUM(Context context) {
        mContext = context;
    }

    /**
     * 是否开启开发环境
     *
     * @author Chenlei
     * created at 2018/9/28
     **/
    public JvtdUM isDevelopment(boolean enabled) {
        UMConfigure.setLogEnabled(enabled);
        UMConfigure.setEncryptEnabled(!enabled);
        return this;
    }

    /**
     * 初始化
     *
     * @author Chenlei
     * created at 2018/9/28
     **/
    public JvtdUM init(String appKey, String channelValue) {
        UMConfigure.init(mContext, appKey, channelValue, UMConfigure
                .DEVICE_TYPE_PHONE, null);
        mShareApi = UMShareAPI.get(mContext);
        return this;
    }

    /**
     * 开启登录  需在初始化之后进行
     *
     * @author Chenlei
     * created at 2018/9/28
     **/
    public JvtdUM openLogin() {
        UMShareConfig config = new UMShareConfig();
        config.isNeedAuthOnGetUserInfo(true);
        mShareApi.setShareConfig(config);
        return this;
    }

    /**
     * 配置微信 默认 微信微信,微信朋友圈开启  微信收藏关闭
     *
     * @author Chenlei
     * created at 2018/9/28
     **/
    public JvtdUM setWechat(String appId, String appSecret) {
        return setWechat(appId, appSecret, true);
    }

    public JvtdUM setWechat(String appId, String appSecret, boolean isWechat) {
        return setWechat(appId, appSecret, isWechat, true, false);
    }

    public JvtdUM setWechat(String appId, String appSecret, boolean isWechat, boolean isCircle) {
        return setWechat(appId, appSecret, isWechat, isCircle, false);
    }

    public JvtdUM setWechat(String appId, String appSecret, boolean isWechat, boolean isCircle, boolean isFavorite) {
        PlatformConfig.setWeixin(appId, appSecret);
        if (isWechat)
            mShareList.add(SHARE_MEDIA.WEIXIN);
        if (isCircle)
            mShareList.add(SHARE_MEDIA.WEIXIN_CIRCLE);
        if (isFavorite)
            mShareList.add(SHARE_MEDIA.WEIXIN_FAVORITE);
        return this;
    }

    /**
     * 配置QQ 默认开启分享
     *
     * @author Chenlei
     * created at 2018/9/28
     **/
    public JvtdUM setQQ(String appId, String appSecret) {
        return setQQ(appId, appSecret, true);
    }

    public JvtdUM setQQ(String appId, String appSecret, boolean isQQ) {
        PlatformConfig.setQQZone(appId, appSecret);
        if (isQQ)
            mShareList.add(SHARE_MEDIA.QQ);
        return this;
    }

    /**
     * 配置微博 默认开启分享
     *
     * @author Chenlei
     * created at 2018/9/28
     **/
    public JvtdUM setSina(String appId, String appSecert) {
        return setSina(appId, appSecert, "");
    }

    public JvtdUM setSina(String appId, String appSecert, String appUrl) {
        return setSina(appId, appSecert, appUrl, true);
    }

    public JvtdUM setSina(String appId, String appSecert, String appUrl, boolean isSina) {
        PlatformConfig.setSinaWeibo(appId, appSecert, appUrl);
        if (isSina)
            mShareList.add(SHARE_MEDIA.SINA);
        return this;
    }

    /**
     * 获取分享类型 用于有面板分享
     *
     * @author Chenlei
     * created at 2018/9/28
     **/
    private SHARE_MEDIA[] getShareList() {
        return mShareList.toArray(new SHARE_MEDIA[mShareList.size()]);
    }

    /**
     * 友盟分享 QQ和微博 必须配置的东西  配置在activity基类onActivityResult中
     *
     * @author Chenlei
     * created at 2018/9/28
     **/
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        UMShareAPI.get(mContext).onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 分享相关
     *
     * @author Chenlei
     * created at 2018/9/28
     **/
    private ShareAction getShareAction(Activity activity) {
        return new ShareAction(activity)
                .setDisplayList(getInstance(mContext).getShareList());
    }

    //有视图版
    //网络图分享
    public void shareImage(Activity activity, String imageUrl, UMShareListener umShareListener) {
        UMImage umImage = new UMImage(activity, imageUrl);
        shareImage(activity, umImage, umShareListener);
    }

    //本地资源分享
    public void shareImage(Activity activity, @DrawableRes int imageRes, UMShareListener umShareListener) {
        UMImage umImage = new UMImage(activity, imageRes);
        shareImage(activity, umImage, umShareListener);
    }

    //创建好UMImage分享
    public void shareImage(Activity activity, UMImage umImage, UMShareListener umShareListener) {
        getShareAction(activity)
                .withMedia(umImage)
                .setCallback(umShareListener)
                .open();
    }

    //分享网址
    public void shareWeb(Activity activity, String url, String title, String description, String imageUrl, UMShareListener umShareListener) {
        UMImage umImage = new UMImage(activity, imageUrl);
        shareWeb(activity, url, title, description, umImage, umShareListener);
    }

    public void shareWeb(Activity activity, String url, String title, String description, @DrawableRes int imageRes, UMShareListener umShareListener) {
        UMImage umImage = new UMImage(activity, imageRes);
        shareWeb(activity, url, title, description, umImage, umShareListener);
    }

    public void shareWeb(Activity activity, String url, String title, String description, UMImage thumb, UMShareListener umShareListener) {
        UMWeb web = new UMWeb(url);
        web.setTitle(title);//标题
        web.setThumb(thumb);  //缩略图
        web.setDescription(description);//描述
        getShareAction(activity)
                .setCallback(umShareListener)
                .withMedia(web)
                .open();
    }

    //无视图版

    //网络图分享
    public void shareImage(Activity activity, SHARE_MEDIA shareMedia, String imageUrl, UMShareListener umShareListener) {
        UMImage umImage = new UMImage(activity, imageUrl);
        shareImage(activity, shareMedia, umImage, umShareListener);
    }

    //本地资源分享
    public void shareImage(Activity activity, SHARE_MEDIA shareMedia, @DrawableRes int imageRes, UMShareListener umShareListener) {
        UMImage umImage = new UMImage(activity, imageRes);
        shareImage(activity, shareMedia, umImage, umShareListener);
    }

    //创建好UMImage分享
    public void shareImage(Activity activity, SHARE_MEDIA shareMedia, UMImage umImage, UMShareListener umShareListener) {
        new ShareAction(activity)
                .setPlatform(shareMedia)//传入平台
                .withMedia(umImage)
                .setCallback(umShareListener)//回调监听器
                .share();
    }

    //分享网址
    public void shareWeb(Activity activity, SHARE_MEDIA shareMedia, String url, String title, String description, String imageUrl, UMShareListener umShareListener) {
        UMImage umImage = new UMImage(activity, imageUrl);
        shareWeb(activity, shareMedia, url, title, description, umImage, umShareListener);
    }

    public void shareWeb(Activity activity, SHARE_MEDIA shareMedia, String url, String title, String description, @DrawableRes int imageRes, UMShareListener umShareListener) {
        UMImage umImage = new UMImage(activity, imageRes);
        shareWeb(activity, shareMedia, url, title, description, umImage, umShareListener);
    }

    public void shareWeb(Activity activity, SHARE_MEDIA shareMedia, String url, String title, String description, UMImage thumb, UMShareListener umShareListener) {
        UMWeb web = new UMWeb(url);
        web.setTitle(title);//标题
        web.setThumb(thumb);  //缩略图
        web.setDescription(description);//描述
        new ShareAction(activity)
                .withMedia(web)
                .setCallback(umShareListener)
                .setPlatform(shareMedia)
                .share();
    }

    /**
     * 第三方登录相关
     *
     * @author Chenlei
     * created at 2018/9/28
     **/

    public void login(Activity activity,SHARE_MEDIA share_media,UMLoginListener umLoginListener){
        mShareApi.getPlatformInfo(activity, share_media, new UMAuthListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {
                umLoginListener.onStart(share_media);
            }

            @Override
            public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
                String name = map.get("name");
                String openid;
                String unionid = "";
                if (share_media == SHARE_MEDIA.WEIXIN)
                {
                    openid = map.get("openid");
                    unionid = map.get("unionid");
                } else
                    openid = map.get("uid");
                String sex = map.get("gender");
                String img = map.get("iconurl");
                UMUserBean umUserBean = new UMUserBean(name,openid,unionid,sex,img);
                umLoginListener.onComplete(share_media,i,umUserBean);
            }

            @Override
            public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
                umLoginListener.onError(share_media,i,throwable);
            }

            @Override
            public void onCancel(SHARE_MEDIA share_media, int i) {
                umLoginListener.onCancel(share_media,i);
            }
        });
    }
}
